#include <bits/stdc++.h>

using namespace std;

#define N 1000005

int a[N];

deque<int> dq;

int main(){
    int n, k;
    cin >> n >> k;

    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }

    for (int i = 1; i <= n; i++){
        while (!dq.empty() && a[dq.back()] > a[i]){
            dq.pop_back();
        }
        dq.push_back(i);
        
        if (i >= k){
            while (!dq.empty() && dq.front() <= i - k){
                dq.pop_front();
            }
        }

        cout << a[dq.front()] << " ";
    }

    cout << endl;

    while (!dq.empty()){
        dq.pop_front();
    }
    
    for (int i = 1; i <= n; i++){
        if (!dq.empty() && a[dq.back()] < a[i]){
            dq.pop_back();
        }
        dq.push_back(i);

        while (!dq.empty() && dq.front() <= i - k){
            dq.pop_front();
        }

        cout << a[dq.front()] << " ";
    }

    return 0;
}